﻿using WebTimer.ScheduledEvents;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RJ.Ext;
using System.Configuration;
using System.Net;
using System.IO;
using CsQuery;

namespace WebTimer.Event
{
    /// <summary>
    /// 有关通知的计划任务
    /// </summary>
    public class LayuiAdmin版本 : IEvent
    {
        private static string url = "https://www.layui.com/admin/pro/json/menu.js";
        private static string url1 = "https://web.helloci.cn/page/json/menu.js";

        #region IEvent 成员

        EventLogs log = new EventLogs();

        public void Execute(object state)
        {

            log.LogFileName = string.Format("{0}log/LayuiAdmin版本.log", AppDomain.CurrentDomain.BaseDirectory);//日志文件名必须与schedule.config中key对应

            StringBuilder msg = new StringBuilder();

            var index_content=this.LoadURLStream(url);

            var localhost_content = this.LoadURLStream(url1);

            var isv = WebTimer.Common.Utils.IsValidStreamContent(index_content, localhost_content);

            msg.AppendFormat("版本一致：{0}", isv);

            if (!isv)
                WebTimer.Common.Utils.SendMail("LayuiAdmin版本版本变动通知", msg.ToString(), "421643133@qq.com");

            log.WriteFailedLog(msg.ToString());

        }

        #endregion



        #region 获取指定页面的内容
        /// <summary>
        /// 从指定的URL下载页面内容(使用WebRequest)
        /// </summary>
        /// <param name="url">指定URL</param>
        /// <returns></returns>
        private string LoadURLString(string url)
        {
            try
            {
                HttpWebRequest myWebRequest = (HttpWebRequest)WebRequest.Create(url);
                HttpWebResponse myWebResponse = (HttpWebResponse)myWebRequest.GetResponse();
                Stream stream = myWebResponse.GetResponseStream();

                string strResult = "";
                StreamReader sr = new StreamReader(stream, System.Text.Encoding.GetEncoding("utf-8"));
                Char[] read = new Char[256];
                int count = sr.Read(read, 0, 256);
                int i = 0;
                while (count > 0)
                {
                    i += Encoding.UTF8.GetByteCount(read, 0, 256);
                    String str = new String(read, 0, count);
                    strResult += str;
                    count = sr.Read(read, 0, 256);
                }

                return strResult;
            }
            catch
            {
                return null;
            }
        }

        private Stream LoadURLStream(string url)
        {
            try
            {
                HttpWebRequest myWebRequest = (HttpWebRequest)WebRequest.Create(url);
                HttpWebResponse myWebResponse = (HttpWebResponse)myWebRequest.GetResponse();
                Stream stream = myWebResponse.GetResponseStream();

                return stream;
            }
            catch
            {
                return null;
            }
        }



        /// <summary>
        /// 从指定的URL下载页面内容(使用WebClient)
        /// </summary>
        /// <param name="url">指定URL</param>
        /// <returns></returns>
        private string LoadPageContent(string url)
        {
            WebClient wc = new WebClient();
            wc.Credentials = CredentialCache.DefaultCredentials;
            byte[] pageData = wc.DownloadData(url);
            return (Encoding.GetEncoding("utf-8").GetString(pageData));
        }
        #endregion
    }
}
